package com.ykt.converter;

import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.DateUtil;
import com.ykt.common.utils.ModelUtil;
import com.ykt.model.Order;
import com.ykt.model.OrderItem;
import com.ykt.vo.OrderItemVo;
import com.ykt.vo.OrderVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: long.hua
 * @date: 2016-02-17 15:47
 * @since 1.0
 */
public class OrderConvert {


    public static OrderVo convert(Order model){
        if(model == null){
            return null;
        }

        OrderVo vo = new OrderVo();

        BaseConverter.convert(vo, model);
        vo.setOrderNo(model.getStr(Order.order_no));
        vo.setOrderPno(model.getStr(Order.order_pno));
        vo.setOrderType(model.getStr(Order.order_type));
        vo.setAmount(model.getBigDecimal(Order.amount).doubleValue());
        vo.setPayAmount(ModelUtil.d(model, Order.pay_amount));
        vo.setAgentFee(ModelUtil.d(model, Order.agent_fee));
        vo.setStatus(model.getStr(Order.status));
        vo.setShopId(model.getInt(Order.shop_id));
        vo.setShopName(model.getStr(ParamNames.shopName));
        vo.setUserId(model.getInt(Order.user_id));
        vo.setUserName(model.getStr(ParamNames.userName));
        vo.setReceiptName(model.getStr(Order.receipt_name));
        vo.setReceiptPhone(model.getStr(Order.receipt_phone));
        vo.setAddress(model.getStr(Order.address));
        vo.setRemarks(model.getStr(Order.remarks));
        vo.setCommunityName(model.getStr(Order.community_name));
        vo.setCommunityAddress(model.getStr(Order.community_address));
        vo.setCommunityContactsName(model.getStr(Order.community_contacts_name));
        vo.setCommunityContactsPhone(model.getStr(Order.community_contacts_phone));
        vo.setPayTime(DateUtil.ymdhms2(model.getDate(Order.pay_time)));
        vo.setPaySerialNo(model.getStr(Order.pay_serial_no));
        vo.setCreatedTime(DateUtil.ymdhms2(model.getDate(Order.created_time)));
        vo.setDeliveryTime(model.getStr(Order.delivery_time));

        return vo;
    }

    public static List<OrderVo> convertOrderList(List<Order> models) {
        if (models == null || models.isEmpty()) {
            return null;
        }

        List<OrderVo> vos = new ArrayList<>(models.size());

        for (Order entity : models) {
            if (entity != null) {
                vos.add(convert(entity));
            }
        }

        return vos;
    }

    public static OrderItemVo convert(OrderItem model){
        if(model == null){
            return null;
        }

        OrderItemVo vo = new OrderItemVo();
        BaseConverter.convert(vo, model);
        vo.setGoodsId(model.getInt(OrderItem.goods_id));
        vo.setGoodsName(model.getStr(OrderItem.goods_name));
        vo.setGoodsPrice(model.getBigDecimal(OrderItem.goods_price).doubleValue());
        vo.setGoodsCount(model.getInt(OrderItem.goods_count));
        vo.setGoodsUnit(model.getStr(OrderItem.goods_unit));
        vo.setGoodsAmount(model.getBigDecimal(OrderItem.goods_amount).doubleValue());

        return vo;
    }

    public static List<OrderItemVo> convertOderItemList(List<OrderItem> entities){
        if(entities == null || entities.isEmpty()){
            return null;
        }

        List<OrderItemVo> vos = new ArrayList<>(entities.size());

        for (OrderItem entity : entities) {
            if(entity != null){
                vos.add(convert(entity));
            }
        }

        return vos;
    }

}
